﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableFrameRegistryUpdater
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApCableFrameRegistryUpdater : IDisposable
{
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;
  private readonly ICApCableFrameRegistry m_frameRegistry;

  public CApCableFrameRegistryUpdater(
    ICApFLEXCableAttributeViewModel attributeViewModel,
    ICApCableFrameRegistry frameRegistry)
  {
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_frameRegistry = frameRegistry ?? throw new ArgumentNullException(nameof (frameRegistry));
    this.m_attributeViewModel.PropertyWillChange += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyWillChange);
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  public void Dispose()
  {
    this.m_attributeViewModel.PropertyWillChange -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyWillChange);
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  private void UpdateFrameAttachmentRegistry(ITxLocatableObject frame)
  {
    if (frame == null)
      return;
    this.m_frameRegistry.UpdateFrameAttachmentRegistry(frame);
  }

  private void UpdateFrameAttachmentRegistry(ITxLocatableObject frame, ITxObject attachedObject)
  {
    if (frame == null)
      return;
    this.m_frameRegistry.UpdateFrameAttachmentRegistry(frame, attachedObject as ITxLocatableObject);
  }

  private void OnPropertyChange(string propertyName)
  {
    if (!this.m_attributeViewModel.IsFrameParameter(propertyName))
      return;
    switch (propertyName)
    {
      case "StartFrame":
      case "StartAttachPartTo":
        this.UpdateFrameAttachmentRegistry(this.m_attributeViewModel.StartFrame, this.m_attributeViewModel.StartAttachPartTo);
        break;
      case "EndFrame":
      case "EndAttachPartTo":
        this.UpdateFrameAttachmentRegistry(this.m_attributeViewModel.EndFrame, this.m_attributeViewModel.EndAttachPartTo);
        break;
      case "PassNearFrame":
        this.UpdateFrameAttachmentRegistry(this.m_attributeViewModel.PassNearFrame);
        break;
    }
  }

  private void AttributeViewModel_PropertyWillChange(object sender, PropertyChangedEventArgs e)
  {
    this.OnPropertyChange(e.PropertyName);
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    this.OnPropertyChange(e.PropertyName);
  }
}
